package cn.bandeqing.attachment.rpc.impl;

import cn.bandeqing.attachment.entity.Attachment;
import cn.bandeqing.attachment.entity.AttachmentType;
import cn.bandeqing.attachment.req.AttachmentListReq;
import cn.bandeqing.attachment.req.AttachmentUploadReq;
import cn.bandeqing.attachment.req.RemoveReq;
import cn.bandeqing.attachment.resp.AttachmentDetail;
import cn.bandeqing.attachment.rpc.IAttachmentRpc;
import cn.bandeqing.attachment.service.IAttachmentService;
import cn.bandeqing.base.resp.Resp;
import cn.bandeqing.frame.dubbo.DubboProviderFilter;
import cn.bandeqing.frame.exception.CodeException;
import cn.bandeqing.product.entity.Product;
import cn.bandeqing.product.service.IProductService;
import cn.bandeqing.worker.entity.Worker;
import cn.bandeqing.worker.service.IWorkerService;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.util.UpdateEntity;
import io.github.linpeilie.Converter;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboService;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static cn.bandeqing.attachment.entity.table.AttachmentTableDef.ATTACHMENT;

@DubboService
public class AttachmentRpcImpl implements IAttachmentRpc {

    @Resource
    private IAttachmentService attachmentService;
    @Resource
    private IWorkerService workerService;
    @Resource
    private IProductService productService;
    @Resource
    private Converter converter;

    @Transactional
    @Override
    public Resp<Void> upload(AttachmentUploadReq req) {
        Attachment attachment = converter.convert(req, Attachment.class);

        if (StrUtil.isNotBlank(req.getUserName())) {
            attachment.setCreateUserId(req.getObjId());
            attachment.setCreateUserName(req.getUserName());
        }

        Assert.isTrue(attachmentService.save(attachment), () -> new CodeException("attachmentService.save(attachment)"));

        if (AttachmentType.worker_id_card_front_url.equals(attachment.getObjType())) {
            Assert.isTrue(workerService.updateById(Worker.builder()
                    .workerId(req.getObjId())
                    .workerIdCardFrontUrl(req.getFilePath())
                    .updateUserId(ObjUtil.defaultIfNull(req.getUserId(), null))
                    .updateUserName(StrUtil.defaultIfBlank(req.getUserName(), null))
                    .build()), () -> new CodeException("workerService.updateById(Worker.builder().build())"));
        } else if (AttachmentType.worker_id_card_back_url.equals(attachment.getObjType())) {
            Assert.isTrue(workerService.updateById(Worker.builder()
                    .workerId(req.getObjId())
                    .updateUserId(ObjUtil.defaultIfNull(req.getUserId(), null))
                    .updateUserName(StrUtil.defaultIfBlank(req.getUserName(), null))
                    .workerIdCardBackUrl(req.getFilePath()).build()), () -> new CodeException("workerService.updateById(Worker.builder().build())"));
        } else if (AttachmentType.worker_head_img_url.equals(attachment.getObjType())) {
            Assert.isTrue(workerService.updateById(Worker.builder()
                    .workerId(req.getObjId())
                    .updateUserId(ObjUtil.defaultIfNull(req.getUserId(), null))
                    .updateUserName(StrUtil.defaultIfBlank(req.getUserName(), null))
                    .workerHeadImgUrl(req.getFilePath()).build()), () -> new CodeException("workerService.updateById(Worker.builder().build())"));
        } else if (AttachmentType.worker_health_certificate_url.equals(attachment.getObjType())) {
            Assert.isTrue(workerService.updateById(Worker.builder()
                    .workerId(req.getObjId())
                    .updateUserId(ObjUtil.defaultIfNull(req.getUserId(), null))
                    .updateUserName(StrUtil.defaultIfBlank(req.getUserName(), null))
                    .workerHealthCertificateUrl(req.getFilePath()).build()), () -> new CodeException("workerService.updateById(Worker.builder().build())"));
        } else if (AttachmentType.product_main.equals(attachment.getObjType())) {
            Assert.isTrue(productService.updateById(Product.builder()
                    .productId(req.getObjId())
                    .productMainImg(req.getFilePath())
                    .build()), CodeException::new);
        }

        return Resp.success();
    }

    @Override
    public Resp<Void> remove(RemoveReq req) {
        if (ObjUtil.isNotNull(req.getFileId())) {
            attachmentService.removeById(req.getFileId());
        } else {
            attachmentService.remove(QueryWrapper.create()
                    .where(ATTACHMENT.OBJ_TYPE.eq(req.getObjType()))
                    .where(ATTACHMENT.OBJ_ID.eq(req.getObjId())));

            if (AttachmentType.worker_id_card_front_url.getValue().equals(req.getObjType())) {
                Worker worker = UpdateEntity.of(Worker.class, req.getObjId());
                worker.setWorkerIdCardFrontUrl(null);
                workerService.updateById(worker);
            } else if (AttachmentType.worker_id_card_back_url.getValue().equals(req.getObjType())) {
                Worker worker = UpdateEntity.of(Worker.class, req.getObjId());
                worker.setWorkerIdCardBackUrl(null);
                workerService.updateById(worker);
            } else if (AttachmentType.worker_head_img_url.getValue().equals(req.getObjType())) {
                Worker worker = UpdateEntity.of(Worker.class, req.getObjId());
                worker.setWorkerHeadImgUrl(null);
                workerService.updateById(worker);
            } else if (AttachmentType.worker_health_certificate_url.getValue().equals(req.getObjType())) {
                Worker worker = UpdateEntity.of(Worker.class, req.getObjId());
                worker.setWorkerHeadImgUrl(null);
                workerService.updateById(worker);
            } else if (AttachmentType.product_main.getValue().equals(req.getObjType())) {
                Product product = UpdateEntity.of(Product.class, req.getObjId());
                product.setProductMainImg(null);
                productService.updateById(product);
            }

        }
        return Resp.success();
    }

    @Override
    public Resp<List<AttachmentDetail>> list(AttachmentListReq req) {
        List<AttachmentDetail> attachmentDetails = attachmentService.listAs(QueryWrapper.create()
                .where(ATTACHMENT.OBJ_ID.eq(req.getObjId()))
                .where(ATTACHMENT.OBJ_TYPE.eq(req.getObjType())), AttachmentDetail.class);

        return Resp.success(attachmentDetails);
    }

}
